/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 * 注意：
 * 本软件为www.tonetn.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.xunyin.modules.user.rest;

import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.log.Log;
import com.xunyin.modules.user.domain.SysXbAppUser;
import com.xunyin.modules.user.service.SysXbAppUserService;
import com.xunyin.modules.user.service.dto.SysXbAppUserDto;
import com.xunyin.modules.user.service.dto.SysXbAppUserQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author admin
 * @date 2022-10-18
 */
@AllArgsConstructor
@Api(tags = "小程序普通（消费者）用户表管理")
@RestController
@RequestMapping("/api/sysXbAppUser")
public class SysXbAppUserController {

    private final SysXbAppUserService sysXbAppUserService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','sysXbAppUser:list')")
    public void download(HttpServletResponse response, SysXbAppUserQueryCriteria criteria) throws IOException {
        sysXbAppUserService.download(generator.convert(sysXbAppUserService.queryAll(criteria), SysXbAppUserDto.class), response);
    }

    @GetMapping
    @Log("查询小程序普通（消费者）用户表")
    @ApiOperation("查询小程序普通（消费者）用户表")
    @PreAuthorize("@el.check('admin','sysXbAppUser:list')")
    public ResponseEntity getSysXbAppUsers(SysXbAppUserQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(sysXbAppUserService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增小程序普通（消费者）用户表")
    @ApiOperation("新增小程序普通（消费者）用户表")
    @PreAuthorize("@el.check('admin','sysXbAppUser:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody SysXbAppUser resources) {
        return new ResponseEntity<>(sysXbAppUserService.save(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改小程序普通（消费者）用户表")
    @ApiOperation("修改小程序普通（消费者）用户表")
    @PreAuthorize("@el.check('admin','sysXbAppUser:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody SysXbAppUser resources) {
        sysXbAppUserService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除小程序普通（消费者）用户表")
    @ApiOperation("删除小程序普通（消费者）用户表")
    @PreAuthorize("@el.check('admin','sysXbAppUser:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        Arrays.asList(ids).forEach(id -> {
            sysXbAppUserService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
